package com.oneinlet.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by WangZiHe on 2018/6/23
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */

/*
 * 定时任务处理，必须在启动类加上注解@EnableScheduling，开启任务调度
 * **/
@Component
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    //下面的任务每隔5000毫秒，即每隔5秒执行一次。
    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("使用fixedRate执行定时任务，当前时间 {}", dateFormat.format(new Date()));
    }

    //下面的任务每隔3秒开始
    @Scheduled(cron = "0/3 * * * * ?")
    public void reportCurrentTimeByCron() {
        log.info("使用cron表达式执行定时任务，当前时间 {}", dateFormat.format(new Date()));
    }
}
